package com.seasugar.raggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seasugar.raggie.common.R;
import com.seasugar.raggie.model.entity.Employee;
import com.seasugar.raggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    public EmployeeService employeeService;

    /***
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee){
        //1.根据用户名查数据库
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        //添加查询条件
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        //用户名不重复
        Employee emp = employeeService.getOne(queryWrapper);
        //2.没有查到用户
        if(emp==null){
            return R.error("用户名错误");
        }
        //3.将页面提交的密码password进行md5加密处理
        String password=employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //4.密码比对
        if(!emp.getPassword().equals(password))
            return R.error("密码错误");
        //5.员工状态
        if(emp.getStatus()==0)
            return R.error("用户已被禁用");
        //6.登录成功,将用户Id存入session，便于后续操作
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request){
        //清理Session
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 获取员工页面信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page> getAllEmployee(@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize,String name){
        log.info("page=:{},pageSize=:{},name=:{}",page,pageSize,name);
        //构造分页构造器
        Page pageInfo=new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper();
        //添加过滤条件
//        if(name!=null)
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo,queryWrapper);
        //.page后结果自动封装在pageInfo中
        return R.success(pageInfo);
    }

    /**
     * 添加员工
     */
    @PostMapping
    public R addEmployee(HttpServletRequest request,@RequestBody Employee employee){
        String password="123456";
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        log.info("执行添加");
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 根据id获取员工信息
     */
    @GetMapping("{id}")
    //@PathVariable的字符串要和GetMapping的一致，变量名不需要
    public R getEmployeeById(@PathVariable("id") Long id){
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getId,id);
        Employee emp = employeeService.getOne(queryWrapper);
        if(emp!=null)
        return R.success(emp);
        return R.error("查无此人");
    }

    /**
     * 修改员工
     */
    @PutMapping
    public R updateEmployee(HttpServletRequest request,@RequestBody Employee employee){
        Long empId = (Long)request.getSession().getAttribute("employee");
//        employee.setUpdateUser(empId);
//        employee.setUpdateTime(LocalDateTime.now());
        Long id = Thread.currentThread().getId();
        log.info("线程id为:{}",id);
        boolean b = employeeService.updateById(employee);
        if(b)
            return R.success("更新成功");
        else
            return R.error("更新失败");
    }
}
